package com.zhs.study.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhs.study.dao.CategoryDao;
import com.zhs.study.dto.CategoryDto;
import com.zhs.study.model.Category;
import com.zhs.study.service.ICategoryService;
import com.zhs.study.common.Enum.R;
import com.zhs.study.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import javax.annotation.Resource;


/**
 *
 * @author
 * @since 2023-12-19
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements ICategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Override
    public R addOrUpdate(CategoryDto categoryDto) {
        if (BeanUtil.isEmpty(categoryDto)) throw new ServiceException("数据不能为空");
        Category category = new Category();
        BeanUtil.copyProperties(categoryDto,category);
        if (categoryDto.getId() == null){
            categoryDao.insert(category);
            return R.ok("数据插入成功");
        }else {
            categoryDao.updateById(category);
            return R.ok("数据更新成功");
        }
    }
}
